#include "musicslider.h"
#include "ui_musicslider.h"

MusicSlider::MusicSlider(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicSlider)
{
    ui->setupUi(this);
    currentPos = 0;
    maxWidth = this->width();
    moveSlider();
}

void MusicSlider::mousePressEvent(QMouseEvent *event)
{
    currentPos = event->pos().x();
    moveSlider();
}

void MusicSlider::mouseMoveEvent(QMouseEvent *event)
{
    QRect rect = QRect(0, 0, width(), height());
    QPoint pos = event->pos();
    if(!rect.contains(pos))
     {
        return;
     }
    if(event->buttons() == Qt::LeftButton)
     {
        currentPos = event->pos().x();
         if(currentPos < 0)
         {
            currentPos = 0;
         }
         if(currentPos > maxWidth)
         {
             currentPos = maxWidth;
         }
         moveSlider();
    }
}
 void MusicSlider::mouseReleaseEvent(QMouseEvent *event)
 {
    currentPos = event->pos().x();
    moveSlider();
 }
 
 void MusicSlider::moveSlider()
 {   
     ui->outLine->setMaximumWidth(currentPos);
     ui->outLine->setGeometry(0,8, currentPos, 4);
     emit setMusicSliderPosition((float)currentPos/(float)maxWidth);
 }

 void MusicSlider::setMusicSliderPosition(float)
 {

 }
 
MusicSlider::~MusicSlider()
{
    delete ui;
}
